package org.cadet.client.controller;


import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.cadet.client.bean.BeanUserFollowed;
import org.cadet.client.model.UserManagement;

/**
 * @author Nagmani
 *
 */

public class FollowUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FollowUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean status=false;
		PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        BeanUserFollowed objUser = new BeanUserFollowed();
        UserManagement objUserManagement = new UserManagement();
        try{
        	int followUserId = Integer.parseInt(request.getParameter("userId").toString());
            int currentUserId = objUserManagement.getUserIdByName((String)session.getAttribute("user"));
            objUser.setCurrentUser(currentUserId);
            objUser.setFollowUser(followUserId);
            try{
            	objUserManagement.followUser(objUser);
                status = true;
            }
            catch(Exception e){
                status = false;
            }
        }catch(Exception e){
        	status=false;
        }
        if(status){
        	out.print("User Followed");
            request.setAttribute("status", "Done");
        }
        else{
        	out.print("User Not followed");
        	request.setAttribute("status", "Fail");
        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
